<?php
	$chkAll = $this->formCheckbox('chkAll', 0);
	// Tạo search
	$ssFilter = $this->arrParam['ssFilter'];
	$action_link 	= $this->baseUrl($this->currentController . '/filter' . '/loaitin/' . $this->loaitin . '/type/order');
	
	// Tạo tiêu đề dùng để sắp xếp
	$lblId 	= $this->cmsLinkSort('#ID', 'id', $ssFilter, $this->baseUrl(PUBLIC_PATH . '/img/icon'), $action_link);
	$lblTitle = $this->cmsLinkSort('Tiêu đề', 'tieu_de', $ssFilter, $this->baseUrl(PUBLIC_PATH . '/img/icon'), $action_link);
	$lblDes= $this->cmsLinkSort('Mô tả', 'mo_ta', $ssFilter, $this->baseUrl(PUBLIC_PATH . '/img/icon'), $action_link);
	$lblDateCreated= $this->cmsLinkSort('Ngày đăng', 'ngay_dang', $ssFilter, $this->baseUrl(PUBLIC_PATH . '/img/icon'), $action_link);
	
	//Tool bar
	$linkDisableItems = $this->baseUrl($this->currentController . '/delete/actionpage/1/loaitin/' . $this->loaitin);
	$btnDisableItems = $this->cmsButton('Xóa nhiều dòng', $linkDisableItems, $this->baseUrl(PUBLIC_PATH . "/img/toolbar/disable_user.png"), 'submit', 'multidelete');
	
	//Search
	$keywords = $this->formText('keywords', $ssFilter['keywords'], array('class' => 'span3 search-input', 'placeholder' => 'Tìm kiếm'));
	$linkSearch = $this->baseUrl($this->currentController . '/filter' . '/loaitin/' . $this->loaitin . '/type/search/key/1');
	$btnSearch = $this->formButton('search-now', 'Search now', array('onclick' => "OnSubmitForm('" . $linkSearch . "')", 'class'=>'btn '));
	$linkResetSearch = $this->baseUrl($this->currentController . '/filter' . '/loaitin/' . $this->loaitin . '/type/search/key/0');
	$btnResetSearch = $this->formButton('clear-keywords', 'Clear', array('onclick' => "OnSubmitForm('" . $linkResetSearch . "')", 'class'=>'btn'));
	$strSearch = $keywords. ' ' . $btnSearch . ' ' . $btnResetSearch;
	
	$frmVIP = $this->frmVIP;
?>

<script type="text/javascript">
$(function(){
    // add multiple select / deselect functionality
    $("#chkAll").click(function () {
          $('.case').attr('checked', this.checked);
    });
    // if all checkbox are selected, check the selectall checkbox and viceversa
    $(".case").click(function() {
        if($(".case").length == $(".case:checked").length) {
            $("#chkAll").attr("checked", "checked");
        } else {
            $("#chkAll").removeAttr("checked");
        }
    });

    $("a.confirm-dialog").click(function(event){
		$( "#question" ).dialog("open");
	});
    
});
</script>

<div class="block">
	<div class="title">
		<span><?php echo $this->title; ?></span>
	</div>
	<div class="content">
		<div class="wrap-content">
			<div id="toolbar-box" class="toolbar">
				<div class="top-box">
					<div class="top-box">
						<div class="top-box"></div>
					</div>
				</div>
				<div class="m-box">
					<div class="icon-page title-users"></div>
					<?php echo $btnDisableItems;?>
					<div class="toolbar-button">
						<a href="<?php echo $this->baseUrl("/member/dangtinnhatro");?>"> 
							<img src="<?php echo $this->baseUrl(PUBLIC_PATH . "/img/toolbar/new.png");?>" />
							<br /> Thêm mới</a>
					</div>
					<div class="clear"></div>
				</div>
				<div class="bottom-box">
					<div class="bottom-box">
						<div class="bottom-box"></div>
					</div>
				</div>
			</div>
	    
	    	<?php if(count($this->Items) > 0) { ?>
				<form name="appForm" action="" method="POST">
					<!-- Search btn -->
					<p class="search-box input-append">
					<?php	echo $strSearch; ?>
					</p>
					<!-- Total items label -->
					<div class="row-func">
						<div class="number-items">
							<span><?php echo $this->totalItems;?> items</span>
						</div>
					</div>
					<table class="gridview" id="tinraotb">
						<thead>
							<tr>
								<th class="check-column"><?php echo $chkAll;?></th>
								<th width="42px"><?php echo $lblId; ?></th>
								<th><?php echo $lblTitle; ?></th>
								<th><?php echo $lblDes; ?></th>
								<th>Gói tin</th>
								<th><?php echo $lblDateCreated; ?></th>
								<th>Trạng thái</th>
								<th>Thao tác</th>
							</tr>
						</thead>
						<tbody>
							<?php
								$modal = '';
								foreach($this->Items as $key => $val) {
									$id    		 = $val->id;
									$title       = $val->tieu_de;
									$description = $val->mo_ta;
									$vip		 = $val->goi_tin;
									$loaitin     = $val->getLoaiTinObj();
									$goitin      = $val->getGoiTinObj();
									$loaiphong   = $val->getLoaiPhongObj();
									$ngaydang    = $val->getNgay_dang('d-m-Y H:i:s');
									$city        = $val->getThanhPhoObj();
									$district    = $val->getQuanHuyenObj();
									$status      = $val->getTrangThaiStr();
									$dientich	 = $val->dien_tich . ' m<sup>2</sup>';
									$giaphong 	 = number_format($val->gia_phong, 3, ",", ",") . ' VNĐ';
									$giadien 	 = number_format($val->gia_dien, 3, ",", ",") . ' VNĐ/kwh';
									$gianuoc 	 = number_format($val->gia_nuoc, 3, ",", ",") . ' VNĐ/m<sup>3</sup>';
										
									//Link
									$modalLink	 = 'userModel' . $id;
									$chkid   	 = $this->formCheckbox('cid[]', $id, array('class' => 'case'));
									
									$upTinrao = '';
									if ($vip == 4)
										$upTinrao = '<li><a class="" href="#upVIP" onClick="upVIPClick(' . $id . ')" role="button" data-toggle="modal"><i class="cus-star"></i>Up VIP</a></li>';
									
									//Action button
									$action = '<div class="btn-group dropup">
													<button class="btn btn-primary btn-mini" data-toggle="dropdown" >
														<i class="icon-ok-circle icon-white"></i>Chức năng
													</button>
							    					<button class="btn btn-primary btn-mini dropdown-toggle" data-toggle="dropdown">&nbsp;
						                               <span class="caret"></span>
						                            </button>
							    					<ul class="dropdown-menu">
									                	<li><a class="" href="#' . $modalLink . '" role="button" data-toggle="modal"><i class="cus-eye"></i>Xem</a></li>
									                	<li><a class="" href="' . $this->baseUrl('/member/chinhsuatinnhatro/id/' . $id) . '"><i class="cus-pencil"></i>Sửa</a></li>'
									                	.$upTinrao.
									                	'<li><a class="confirm-dialog" onclick="return confirmDialog(this, \'/member/delete/id/' . $id . '/actionpage/1/loaitin/' . $this->loaitin . '\');" href="#"><i class="cus-bin_closed"></i>Xóa</a></li>
									                </ul>
									           </div>';
										
									$detailinfo ='';
									$detailinfo .= '<div id="tinrao'.$id.'"><table class="dialog_table table table-hover table-bordered">';
									$detailinfo .="<tr class='odd'><th>Tiêu đề:</th><td>" . $title . "</td></tr>";
									$detailinfo .="<tr class='even'><th>Ngày đăng:</th><td>" . $ngaydang . "</td></tr>";
									$detailinfo .="<tr class='even'><th>Mô tả:</th><td>" . $description . "</td></tr>";
									$detailinfo .="<tr class='even'><th>Địa chỉ:</th><td>" . $val->dia_chi . "</td></tr>";
									$detailinfo .="<tr class='even'><th>Quận huyện - TP:</th><td>" . $district->ten . " - " . $city->ten . "</td></tr>";
									$detailinfo .="<tr class='odd'><th>Loại tin:</th><td>" . $loaitin->ten . "</td></tr>";
									$detailinfo .="<tr class='even'><th>Gói tin:</th><td>" . $goitin->ten . "</td></tr>";
									$detailinfo .="<tr class='odd'><th>Loại phòng:</th><td>" . $loaiphong->ten . "</td></tr>";
									$detailinfo .="<tr class='odd'><th>Giá phòng:</th><td>" . $dientich . "</td></tr>";
									$detailinfo .="<tr class='odd'><th>Giá phòng:</th><td>" . $giaphong . "</td></tr>";
									$detailinfo .="<tr class='odd'><th>Giá điện:</th><td>" . $giadien . "</td></tr>";
									$detailinfo .="<tr class='odd'><th>Giá nước:</th><td>" . $gianuoc . "</td></tr>";
									$detailinfo .= "</table></div>";
						
									$modal .='<div id="' . $modalLink . '" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
					  							<div class="modal-header">
											    	<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
											    	<h4 id="myModalLabel">Thông tin chi tiết tin rao <span>' . $id . '</span></h4>
											  	</div>
											  	<div class="modal-body">'.
													$detailinfo.
												'</div>
											  </div>';
										
									$class = '';
									if ($vip == 1) $class = 'class="vip1"';
									echo '<tr id=' . $id . ' ' . $class . '>';
									echo '<th class="check-column" scope="row">' . $chkid . '</th>';
									echo '<td align="center">' . $id . '</td>';
									echo '<td title="' . $title . '">' . Zendhlt_Encode::truncateString_($title, 20) . '</td>';
									echo '<td title="' . $description . '">' . Zendhlt_Encode::truncateString_($description, 20) . '</td>';
									echo '<td align="center">' . $goitin->ten . '</td>';
									echo '<td class="cell-padding">' . $ngaydang . '</td>';
									echo '<td class="cell-padding">' . $status . '</td>';
									echo '<td align="center">' . $action.'</td>';
									echo '</tr>';
								}
							?>
						</tbody>
						<!-- BEGIN: PAGINATION (FOOT) -->
						<tfoot>
							<tr>
								<td colspan="8">
									<div class="pagination">
										<?php
											$currentUrl = $this->baseUrl($this->currentController . '/ptrtinrao/loaitin/' . $this->loaitin);
											echo $this->paginationControl($this->panigator, 'Sliding', 'pagination.phtml', array('currentUrl' => $currentUrl));
										?>
									</div>
								</td>
							</tr>
						</tfoot>
						<!-- END: PAGINATION (FOOT) -->
					</table>
					<div class="clear"></div>
				</form>
				<div class="dialog" id="question">
					<img src="<?php echo $this->baseUrl(PUBLIC_PATH . '/img/question.png'); ?>" alt="question" />
					<p>Bạn có chắc chắn muốn xóa không?</p>
				</div>
				<?php echo $modal;?>
				<div id="upVIP" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
						<h3 id="myModalLabel">Mua gói VIP</h3>
					</div>
					<div class="modal-body">
						<div id="upVIPalert" class="alert"></div>
						<?php echo '<form id="' . $frmVIP->getId() . '" method="' . $frmVIP->getMethod() . '" action="" enctype="' . $frmVIP->getEnctype() . '" name="' . $frmVIP->getName() . '">'; ?>
							<?php echo $frmVIP->idtin;?>
							<dl class="control-group">
								<dt><label class="control-label" for="ten_goi">Gói VIP</label></dt>
								<dd class="controls">
								<?php echo $frmVIP->ten_goi;?>
								</dd>
								<dt><label class="control-label" for="gia">Giá</label></dt>
								<dd class="controls">
								<?php echo $frmVIP->gia;?>
								</dd>
								<dt><label class="control-label" for="so_ngay">Thời hạn (ngày)</label></dt>
								<dd class="controls">
								<?php echo $frmVIP->so_ngay;?>
								</dd>
							</dl>
						<?php echo '</form>'; ?>
					</div>
					<div class="modal-footer">
						<button class="btn" data-dismiss="modal" aria-hidden="true">Đóng</button>
						<button id="btnAgree" class="btn btn-primary">Đồng ý</button>
					</div>
				</div>
			<?php } else { echo '<p class="no-info">Chưa có dữ liệu nào được đăng.</p>'; }?>
			<br class="clear" />
		</div>
	</div>
	<br class="clear" />
</div>
<script type="text/javascript">
	//Catch click event of the edit button
	$('#btnAgree').click(function(e) {
		upVIPProcess(e);
	});

	//Process up VIP function
	function upVIPProcess(event) {
		var url = '/member/buyvip/loai/1';
		var postData = $("#upVIP form").serializeObject();
		postData.format = "json";
    	$('#btnAgree').attr("disabled", "disabled");
    	$.ajax({
            type: 'POST',
            url: url,
            async: false,
            cache: true,
            data: postData,
            dataType: postData.format,
            success: function(data){
        		$('#upVIPalert').html(data.message);
        		$('#upVIPalert').show();
            	if(data.edit) {
            		setTimeout(function() {
						$('#upVIP').modal('hide'); 
						window.location.reload();
					}, 500);
            	}
            }
        });
    	$('#btnAgree').removeAttr("disabled");
	}
	
	function upVIPClick(id) {
		$('#idtin').val(id);
		$('#upVIPalert').hide();
	}

	function getGoiTin() {
				
		var id = $('#ten_goi').val();
		var url = '/ajax/goivip';
		$.ajax({
			'url':url,
			'type':'post',
			'data':{'id':id},
			'success':function(response) {
				var data = $.parseJSON(response);
			    $('#gia').val(data[0]);
			    $('#so_ngay').val(data[1]);
			}
		});
	}
</script>